package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lzx.api.CommonResult;
import com.lzx.dto.AdminDto;
import com.lzx.entity.Admin;
import com.lzx.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2021-03-03 14:41:21
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Admin selectOne(Integer id) {
        return this.adminService.queryById(id);
    }

    /**
     * 管理员登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public CommonResult<Admin> login(String username,String password){
        if(null==username||null==password){
            return CommonResult.failed("用户名和密码不能为空");
        }
        Admin loginAdmin = adminService.login(username, password);
        if (null==loginAdmin){
            return CommonResult.failed("用户名或密码错误");
        }
        return CommonResult.success(loginAdmin);
    }
    @PostMapping("/add")
    public CommonResult<Admin> add(@RequestBody AdminDto admin){
        Admin newAdmin = new Admin();
        if(BeanUtil.isEmpty(admin)){
            return CommonResult.failed();
        }
        BeanUtil.copyProperties(admin,newAdmin);
        adminService.insert(newAdmin);
        return CommonResult.success(200,"添加成功");
    }

}
